package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.operation.CreateOperationDTO;
import com.szly.phm.common.entity.dto.operation.LogPageDTO;
import com.szly.phm.common.entity.dto.operation.OperationPageDTO;
import com.szly.phm.common.entity.dto.operation.UpdateOperationDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.SysLog;
import com.szly.phm.common.entity.po.SysOperation;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.mapper.SysLogMapper;
import com.szly.phm.common.mapper.SysOperationMapper;
import com.szly.phm.common.service.ISysOperationService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-09-19
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysOperationServiceImpl extends ServiceImpl<SysOperationMapper, SysOperation> implements ISysOperationService {
    private  String[] type=new String[]{"登录","添加","删除","修改"};
    private final SysLogMapper sysLogMapper;
    @Override
    public RootResponse createOperation(CreateOperationDTO createOperationDTO, LoginUserDTO loginUserDTO) {
        var sysOperation=new SysOperation();
        BeanCopyUtils.copy(createOperationDTO,sysOperation);
        sysOperation.setId(HandleUtils.UUID32());
        sysOperation.setCreator(loginUserDTO.getUsername());
        sysOperation.setCreateDateTime(new Date());
        sysOperation.setIsSoftDelete(false);
        int i =  baseMapper.insert(sysOperation);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse updateOperation(UpdateOperationDTO updateOperationDTO, LoginUserDTO loginUserDTO) {
        var sysOperation=new SysOperation();
        BeanCopyUtils.copy(updateOperationDTO,sysOperation);
        var wrapper1 = new LambdaQueryWrapper<SysOperation>();
        wrapper1.eq(SysOperation::getId, updateOperationDTO.getId());
        wrapper1.eq(SysOperation::getIsSoftDelete, false);
        var sysOperation1 = baseMapper.selectOne(wrapper1);
        if (sysOperation1 == null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
//        if (!updateOperationDTO.getRouter().equals(sysOperation1.getRouter())) {
//            wrapper1.clear();
//            wrapper1.eq(SysOperation::getIsSoftDelete, false);
//            wrapper1.and(wrapper->wrapper.eq(SysOperation::getRouter, updateOperationDTO.getRouter()).or().eq(SysOperation::getName, updateOperationDTO.getName()));
//            var permission2 = baseMapper.selectOne(wrapper1);
//            if (permission2 != null) {
//                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "路由已存在，请勿重复添加");
//            }
//        }
        int i =  baseMapper.updateById(sysOperation);
        return i == 1 ? RootResponse.success(baseMapper.selectById(updateOperationDTO.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse queryOperationPageV1(OperationPageDTO operationPageDTO) {
        var wrapper = new LambdaQueryWrapper<SysOperation>();
        wrapper.eq(SysOperation::getIsSoftDelete, false);
        wrapper.eq(SysOperation::getPageId, operationPageDTO.getPageId());
        wrapper.orderByDesc(SysOperation::getCreateDateTime);
//        if ("00".equals(permissionDTO.getType())) {
        PageHelper.startPage(operationPageDTO.getPageNum(), operationPageDTO.getPageSize());
        var operations = baseMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(operations);
        var pageList = pageInfo.getList();
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
//        } else {
//            var permissions = baseMapper.selectList(wrapper);
//            return RootResponse.success(permissions);
//        }
    }

    @Override
    public RootResponse deleteOperation(String id, LoginUserDTO loginUserDTO) {
      var sysOperation=  new SysOperation();
      sysOperation.setUpdater(loginUserDTO.getUsername());
      sysOperation.setUpdateDateTime(new Date());
      sysOperation.setId(id);
      sysOperation.setIsSoftDelete(true);
        int i =  baseMapper.updateById(sysOperation);
        return i == 1 ? RootResponse.success(baseMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse queryLoginPageV1(LogPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        var wrapper = new LambdaQueryWrapper<SysLog>();
        wrapper.eq(SysLog::getIsSoftDelete, false);
        if(StringUtils.isNotBlank(pageDTO.getEndTime())&&StringUtils.isNotBlank(pageDTO.getStartTime())){
            wrapper.le(SysLog::getCreateDateTime,pageDTO.getEndTime()).ge(SysLog::getCreateDateTime,pageDTO.getStartTime());
        }
        if(pageDTO.getType()!=null&&pageDTO.getType()<4&&-1<pageDTO.getType()){
            wrapper.eq(SysLog::getLogType,type[pageDTO.getType()]);
        }
        if(StringUtils.isNotBlank(pageDTO.getIp())){
            wrapper.like(SysLog::getIpAddress,pageDTO.getIp());
        }
        wrapper.orderByDesc(SysLog::getCreateDateTime);
        var selectList = sysLogMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(selectList);
        var pageList = pageInfo.getList();
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }


}
